import { useState, useEffect } from 'react'
import { message } from 'antd'
import { requestOne, requestTwo, requestThree, requestFour } from './ajax'
import { awaitWrap } from '@js/tool'

// 获取超审限数据
async function getOverTime (setNum, setLoading) {
  setLoading(true)
  const [e, d] = await awaitWrap(requestOne())
  setLoading(false)
  if (e) {
    return message.warning(e.message)
  }
  setNum(d)
}

// 超审限
export function useOverTime () {
  const [num, setNum] = useState(0)
  const [loading, setLoading] = useState(false)
  useEffect(() => {
    getOverTime(setNum, setLoading)
  }, [])
  return [num, loading]
}

// 获取延期组庭数据
async function getGroupOverTime (setNum, setLoading) {
  setLoading(true)
  const [e, d] = await awaitWrap(requestTwo())
  setLoading(false)
  if (e) {
    return message.warning(e.message)
  }
  setNum(d)
}

// 延期组庭
export function useGroupOverTime () {
  const [num, setNum] = useState(0)
  const [loading, setLoading] = useState(false)
  useEffect(() => {
    getGroupOverTime(setNum, setLoading)
  }, [])
  return [num, loading]
}

// 获取中止案件数据
async function getSuspension (setNum, setLoading) {
  setLoading(true)
  const [e, d] = await awaitWrap(requestFour())
  setLoading(false)
  if (e) {
    return message.warning(e.message)
  }
  setNum(d)
}

// 中止案件
export function useSuspension () {
  const [num, setNum] = useState(0)
  const [loading, setLoading] = useState(false)
  useEffect(() => {
    getSuspension(setNum, setLoading)
  }, [])
  return [num, loading]
}

// 获取审限延长
async function getExtend (setNum, setLoading) {
  setLoading(true)
  const [e, d] = await awaitWrap(requestThree())
  setLoading(false)
  if (e) {
    return message.warning(e.message)
  }
  setNum(d)
}

// 审限延长
export function useExtend () {
  const [map, setMap] = useState({ 0: 0, 1: 0, 2: 0, 3: 0, 4: 0 })
  const [loading, setLoading] = useState(false)
  useEffect(() => {
    getExtend(setMap, setLoading)
  }, [])
  return [map, loading]
}
